package com.netease.nim.demo.session.helper;

import android.text.TextUtils;

import com.netease.nim.demo.NimCache;
import com.netease.nim.demo.contact.cache.TeamDataCache;
import com.netease.nim.demo.team.helper.AnnouncementHelper;
import com.netease.nim.demo.team.model.Announcement;
import com.netease.nimlib.sdk.msg.attachment.NotificationAttachment;
import com.netease.nimlib.sdk.msg.constant.SessionTypeEnum;
import com.netease.nimlib.sdk.msg.model.IMMessage;
import com.netease.nimlib.sdk.team.constant.TeamFieldEnum;
import com.netease.nimlib.sdk.team.constant.VerifyTypeEnum;
import com.netease.nimlib.sdk.team.model.MemberChangeAttachment;
import com.netease.nimlib.sdk.team.model.UpdateTeamAttachment;
import com.shwread.android.qysw10000136.R;

import java.util.List;

/**
 * Created by huangjun on 2015/3/11.
 */
public class MessageHelper {
    private static ThreadLocal<String> teamId = new ThreadLocal<String>();

    public static String getMsgShowText(final IMMessage message) {
        String content = "";
        String messageTip = message.getMsgType().getSendMessageTip();
        if (messageTip.length() > 0) {
            content += "[" + messageTip + "]";
        } else {
            if (message.getSessionType() == SessionTypeEnum.Team && message.getAttachment() != null) {
                content += getTeamNotificationText(message, message.getSessionId());
            } else {
                content += message.getContent();
            }
        }

        return content;
    }

    public static String getTeamNotificationText(IMMessage message, String tid) {
        return getTeamNotificationText(message.getSessionId(), message.getFromAccount(), (NotificationAttachment) message.getAttachment());
    }

    public static String getTeamNotificationText(String tid, String fromAccount, NotificationAttachment attachment) {
        teamId.set(tid);
        String text = buildNotification(fromAccount, attachment);
        teamId.set(null);
        return text;
    }

    private static String buildNotification(String fromAccount, NotificationAttachment attachment) {
        String text;
        switch (attachment.getType()) {
        case InviteMember:
            text = buildInviteMemberNotification(((MemberChangeAttachment) attachment), fromAccount);
            break;
        case KickMember:
            text = buildKickMemberNotification(((MemberChangeAttachment) attachment));
            break;
        case LeaveTeam:
            text = buildLeaveGroupNotification(fromAccount);
            break;
        case DismissTeam:
            text = buildDismissGroupNotification(fromAccount);
            break;
        case UpdateTeam:
            text = buildUpdateGroupNotification((UpdateTeamAttachment) attachment);
            break;
        case PassTeamApply:
            text = buildManagerPassTeamApplyNotification((MemberChangeAttachment) attachment);
            break;
        case TransferOwner:
            text = buildTransferOwnerNotification(fromAccount, (MemberChangeAttachment) attachment);
            break;
        case AddTeamManager:
            text = buildAddTeamManagerNotification((MemberChangeAttachment) attachment);
            break;
        case RemoveTeamManager:
            text = buildRemoveTeamManagerNotification((MemberChangeAttachment) attachment);
            break;
        case AcceptInvite:
            text = buildAcceptInviteNotification(fromAccount, (MemberChangeAttachment) attachment);
            break;
        default:
            text = getTeamMemberDisplayName(fromAccount) + ": unknown message";
            break;
        }

        return text;
    }

    private static String getTeamMemberDisplayName(String account) {
        return TeamDataCache.getInstance().getDisplayNameWithoutMe(teamId.get(), account);
//        return TeamDataCache.getInstance().getTeamMemberDisplayNameYou(teamId.get(), account);
    }

    private static String buildMemberListString(List<String> members, String fromAccount) {
        StringBuilder sb = new StringBuilder();
        for (String uid : members) {
            if (!TextUtils.isEmpty(fromAccount) && fromAccount.equals(uid)) {
                continue;
            }
            sb.append(getTeamMemberDisplayName(uid));
            sb.append(",");
        }
        sb.deleteCharAt(sb.length() - 1);

        return sb.toString();
    }

    private static String buildInviteMemberNotification(MemberChangeAttachment a, String fromAccount) {
        StringBuilder sb = new StringBuilder();
        String selfName = getTeamMemberDisplayName(fromAccount);

        sb.append(selfName);
        sb.append("邀请 ");
        sb.append(buildMemberListString(a.getTargets(), fromAccount));
        sb.append(" 加入群");

        return sb.toString();
    }

    private static String buildKickMemberNotification(MemberChangeAttachment a) {
        StringBuilder sb = new StringBuilder();
        sb.append(buildMemberListString(a.getTargets(), null));
        sb.append(" 已被移出群");

        return sb.toString();
    }

    private static String buildLeaveGroupNotification(String fromAccount) {
        return getTeamMemberDisplayName(fromAccount) + " 离开了群";
    }

    private static String buildDismissGroupNotification(String fromAccount) {
        return getTeamMemberDisplayName(fromAccount) + " 解散了群";
    }

    private static String buildUpdateGroupNotification(UpdateTeamAttachment a) {
        if (a.getField() == TeamFieldEnum.Name) {
            return "群名称被更新为 " + a.getValue();
        } else if (a.getField() == TeamFieldEnum.Introduce) {
            return "群介绍被更新为 " + a.getValue();
        } else if (a.getField() == TeamFieldEnum.Announcement) {
            return "群公告被更新为 " + getAnnouncement(a.getValue());
        } else if (a.getField() == TeamFieldEnum.VerifyType) {
            VerifyTypeEnum type = (VerifyTypeEnum) a.getValue();
            String authen = "群身份验证权限更新为";
            if (type == VerifyTypeEnum.Free) {
                return authen + NimCache.getContext().getString(R.string.team_allow_anyone_join);
            } else if (type == VerifyTypeEnum.Apply) {
                return authen + NimCache.getContext().getString(R.string.team_need_authentication);
            } else {
                return authen + NimCache.getContext().getString(R.string.team_not_allow_anyone_join);
            }
        } else if (a.getField() == TeamFieldEnum.Extension) {
            return "群扩展字段被更新为 " + a.getValue();
        } else if (a.getField() == TeamFieldEnum.Ext_Server) {
            return "群扩展字段(服务器)被更新为 " + a.getValue();
        } else {
            return "群" + a.getField() + "被更新为 " + a.getValue();
        }
    }

    private static String buildManagerPassTeamApplyNotification(MemberChangeAttachment a) {
        StringBuilder sb = new StringBuilder();
        sb.append("管理员通过用户 ");
        sb.append(buildMemberListString(a.getTargets(), null));
        sb.append(" 的入群申请");

        return sb.toString();
    }

    private static String buildTransferOwnerNotification(String from, MemberChangeAttachment a) {
        StringBuilder sb = new StringBuilder();
        sb.append(getTeamMemberDisplayName(from));
        sb.append(" 将群转移给 ");
        sb.append(buildMemberListString(a.getTargets(), null));

        return sb.toString();
    }

    private static String buildAddTeamManagerNotification(MemberChangeAttachment a) {
        StringBuilder sb = new StringBuilder();

        sb.append(buildMemberListString(a.getTargets(), null));
        sb.append(" 被任命为管理员");

        return sb.toString();
    }

    private static String buildRemoveTeamManagerNotification(MemberChangeAttachment a) {
        StringBuilder sb = new StringBuilder();

        sb.append(buildMemberListString(a.getTargets(), null));
        sb.append(" 被撤销管理员身份");

        return sb.toString();
    }

    private static String buildAcceptInviteNotification(String from, MemberChangeAttachment a) {
        StringBuilder sb = new StringBuilder();

        sb.append(getTeamMemberDisplayName(from));
        sb.append(" 接受了 ").append(buildMemberListString(a.getTargets(), null)).append(" 的入群邀请");

        return sb.toString();
    }

    private static String getAnnouncement(Object value) {
        Announcement a = AnnouncementHelper.getLastAnnouncement((String) value);
        return a == null ? null : a.getTitle();
    }
}
